package com.watermeter.android.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.oa.android.R;
import com.watermeter.android.activity.ChangeInfoActivity;
import com.watermeter.android.activity.LurucbActivity;
import com.watermeter.android.entry.DownRoute;

import java.util.List;

/**
 * Created by zouqh on 2016/5/23.
 */
public class RouteItemAdapter extends BaseAdapter {
    private Context context;
    private List<DownRoute> rowses;

    public RouteItemAdapter(Context context, List<DownRoute> rowses) {
        this.context = context;
        this.rowses = rowses;
    }

    @Override
    public int getCount() {
        return rowses.size();
    }

    @Override
    public Object getItem(int position) {
        return rowses.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHodler viewHodler;
        if (convertView == null) {
            viewHodler = new ViewHodler();
            convertView = LayoutInflater.from(context).inflate(R.layout.route_item_item, null);
            viewHodler.code = (TextView) convertView.findViewById(R.id.code);
            viewHodler.userName = (TextView) convertView.findViewById(R.id.userName);
            viewHodler.cbxh = (TextView) convertView.findViewById(R.id.cbxh);
            viewHodler.byds = (TextView) convertView.findViewById(R.id.byds);

            viewHodler.cbBtn = (Button) convertView.findViewById(R.id.bcBtn);
            viewHodler.changeBtn = (Button) convertView.findViewById(R.id.changeBtn);
            viewHodler.byyl = (TextView) convertView.findViewById(R.id.byyl);
            viewHodler.states = (TextView) convertView.findViewById(R.id.states);
            viewHodler.syds = (TextView) convertView.findViewById(R.id.syds);
            viewHodler.address = (TextView) convertView.findViewById(R.id.address);
           viewHodler.sbbh = (TextView) convertView.findViewById(R.id.sbbh);
            viewHodler.tsMsg = (TextView) convertView.findViewById(R.id.tsMsg);
            viewHodler.yslx=(TextView)convertView.findViewById(R.id.yslx);
            convertView.setTag(viewHodler);
        } else {
            viewHodler = (ViewHodler) convertView.getTag();
        }
        int a=rowses.get(position).getSBZD();
        int b=Integer.parseInt(rowses.get(position).getSBQD());
        viewHodler.userName.setText("用户名：" + rowses.get(position).getUSERNAME());
        viewHodler.code.setText("客户编号：" + rowses.get(position).getUSERID());
        viewHodler.byds.setText("本月读数：" + a);
        viewHodler.syds.setText("上月读数：" +b);
        viewHodler.yslx.setText("用水类型："+rowses.get(position).getPRIMEMO());
        viewHodler.address.setText("详细地址：" + rowses.get(position).getXXDZ());
        viewHodler.sbbh.setText("水表编号：" + rowses.get(position).getSBBH());
        if (null!=rowses.get(position).getSHINT()&&rowses.get(position).getSHINT().length()>0){
            viewHodler.tsMsg.setText("提示："+rowses.get(position).getSHINT());
        }else{
            viewHodler.tsMsg.setText("");
        }

        if(a>0){
            viewHodler.byyl.setText("本月用量："+(a-b));
        }else{
            viewHodler.byyl.setText("本月用量：0");
        }

        if(rowses.get(position).getChange()>0){

            if(rowses.get(position).getUpdate()>0){
                viewHodler.states.setText("已经上传");
                viewHodler.states.setTextColor(Color.parseColor("#FFFF4A3D"));
            }else{
                viewHodler.states.setText("已抄表");
                viewHodler.states.setTextColor(Color.parseColor("#FF00ADFF"));
            }

        }else{
            viewHodler.states.setText("未抄表");
            viewHodler.states.setTextColor(Color.parseColor("#eb535452"));
        }


        viewHodler.cbxh.setText("抄表序号："+rowses.get(position).getCBXH() );
        viewHodler.cbBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, LurucbActivity.class);
                intent.putExtra("id",position+"");
                context.startActivity(intent);
            }
        });
        viewHodler.changeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, ChangeInfoActivity.class);
                intent.putExtra("id",position+"");
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHodler {
        TextView userName;
        TextView code;
        TextView byds;
        TextView byyl;
        TextView syds;
        Button cbBtn;
        Button changeBtn;
        TextView address;
        TextView cbxh;
        TextView states;
        TextView sbbh;
        TextView tsMsg;
        TextView yslx;

    }
}
